<?php

class ConfigFile extends IniFile
{
	/**
	 * This function reads the data from the ini file. Its a wrapper around parse_ini_file, with sections enabled.
	 *
	 */
	public function read()
	{
		if(file_exists($this->path) && is_readable($this->path))
		{
			include($this->path);
			$this->content = isset($content) ? $content : array();
		}else{
			$this->content = array();
		}
	}

	/**
	 * This function saves back the current data to the iniFile, if that data has changed.
	 *
	 * @return bool
	 */
	public function write()
	{
		if(file_exists($this->path))
		{
			$oldConfig = new ConfigFile($this->path);
			$oldContents = $oldConfig->getArray();
			if($oldContents == $this->content)
				return true;
		}

		$string = '<?php' . PHP_EOL;
		$string .= '// File generated by Mortar ConfigFile' . PHP_EOL;
		$string .= '// Last saved ' . date("F j, Y \a\\t g:i a") . PHP_EOL . PHP_EOL;

		foreach($this->content as $section => $settings)
		{
			$string .= '// ' . $section . PHP_EOL . PHP_EOL;
			foreach($settings as $name => $value)
			{
				$string .= "\$content['$section']['$name'] = " . $this->writeValue($value) . ';' . PHP_EOL;
				//$string .= $name . ' = ' . $this->writeValue($value) . PHP_EOL;
			}
			$string .= PHP_EOL . PHP_EOL;
		}

		return file_put_contents($this->path, $string);
	}
}

?>